﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using iTeachSoft.CoreObjects;
using iTeachSoft.Code.Utility;
using iTeachSoft.Presentation.Code.Managers;

namespace iTeachSoft.Presentation.Dialogs
{
    /// <summary>
    /// Interaction logic for DialogRectangleWithText.xaml
    /// </summary>
    public partial class DialogRectangleWithText : Window
    {
        private RectangleWithText mCoreObject;

        public RectangleWithText CoreObject
        {
            get { return mCoreObject; }
            set
            {
                mCoreObject = value;
                ReflectCoreObject();
            }
        }

        public DialogRectangleWithText()
        {
            InitializeComponent();

            this.buttonOk.Click += new RoutedEventHandler(buttonOk_Click);
            this.buttonCancel.Click += new RoutedEventHandler(buttonCancel_Click);

            this.borderBackgroundColor.MouseDown += new MouseButtonEventHandler(borderBackgroundColor_MouseDown);
            this.borderBorderBrushColor.MouseDown += new MouseButtonEventHandler(borderBorderBrushColor_MouseDown);
            this.borderFontColor.MouseDown += new MouseButtonEventHandler(borderFontColor_MouseDown);
        }        

        protected void ReflectCoreObject()
        {
            textBoxName.Text = CoreObject.Name;
            textBoxWidth.Text = CoreObject.Bounds.Width.ToString();
            textBoxHeight.Text = CoreObject.Bounds.Height.ToString();
            
            borderBackgroundColor.Background = new SolidColorBrush(Color.FromArgb(CoreObject.BackColor.A, CoreObject.BackColor.R, CoreObject.BackColor.G, CoreObject.BackColor.B));
            borderBorderBrushColor.Background = new SolidColorBrush(Color.FromArgb(CoreObject.Stroke.A, CoreObject.Stroke.R, CoreObject.Stroke.G, CoreObject.Stroke.B));
            borderFontColor.Background = new SolidColorBrush(Color.FromArgb(CoreObject.FontColor.A, CoreObject.FontColor.R, CoreObject.FontColor.G, CoreObject.FontColor.B));

            textBoxXRadius.Text = CoreObject.RadiusX.ToString();
            textBoxYRadius.Text = CoreObject.RadiusY.ToString();

            textBoxOpacity.Text = ((float)((float)CoreObject.BackColor.A / (float)255)).ToString("0.00");
            textBoxBorderThikness.Text = CoreObject.BorderWeight.ToString();

            textBoxFontSize.Text = CoreObject.FontSize.ToString();
            comboBoxFontFamily.SelectedValue = new FontFamily(CoreObject.FontFamilyName);

            richTextBoxText.Text = CoreObject.Text;
        }

        protected void UpdateCoreObject(RectangleWithText coreObject)
        {
            coreObject.Name = textBoxName.Text;
            int width = -1;
            int.TryParse(textBoxWidth.Text, out width);
            if (width < 0)
                AlertUtility.ShowError("Please enter a valid width for the selected object!");

            int height = -1;
            int.TryParse(textBoxHeight.Text, out height);
            if (height < 0)
                AlertUtility.ShowError("Please enter a valid height for the selected object!");

            if (width * height > 0)
                coreObject.Bounds = new System.Drawing.Rectangle(coreObject.Bounds.Location, new System.Drawing.Size(width, height));

            int opacity = (int)(double.Parse(textBoxOpacity.Text) * 255);

            Color backColor = ((SolidColorBrush)borderBackgroundColor.Background).Color;
            coreObject.BackColor = System.Drawing.Color.FromArgb(opacity, backColor.R, backColor.G, backColor.B);

            Color borderColor = ((SolidColorBrush)borderBorderBrushColor.Background).Color;
            coreObject.Stroke = System.Drawing.Color.FromArgb(borderColor.A, borderColor.R, borderColor.G, borderColor.B);

            Color fontColor = ((SolidColorBrush)borderFontColor.Background).Color;
            coreObject.FontColor = System.Drawing.Color.FromArgb(fontColor.A, fontColor.R, fontColor.G, fontColor.B);

            coreObject.RadiusX = int.Parse(textBoxXRadius.Text);
            coreObject.RadiusY = int.Parse(textBoxYRadius.Text);
            coreObject.FontSize = int.Parse(textBoxFontSize.Text);

            var family = (FontFamily)comboBoxFontFamily.SelectedValue;
            coreObject.FontFamilyName = family.Source;
            coreObject.Text = richTextBoxText.Text;

            coreObject.BorderWeight = int.Parse(textBoxBorderThikness.Text);

            EventsManagement.Instance.OnReflectTreeViewRequested(this, EventArgs.Empty);
            EventsManagement.Instance.OnReflectSlideViewerRequested(this, EventArgs.Empty);
        }

        void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            UpdateCoreObject(mCoreObject);
            this.Close();
        }

        void borderBorderBrushColor_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.ColorDialog colorPicker = new System.Windows.Forms.ColorDialog();
            if (colorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                borderBorderBrushColor.Background = new SolidColorBrush(Color.FromArgb(colorPicker.Color.A, colorPicker.Color.R, colorPicker.Color.G, colorPicker.Color.B));
        }

        void borderBackgroundColor_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.ColorDialog colorPicker = new System.Windows.Forms.ColorDialog();
            if (colorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                borderBackgroundColor.Background = new SolidColorBrush(Color.FromArgb(colorPicker.Color.A, colorPicker.Color.R, colorPicker.Color.G, colorPicker.Color.B));
        }

        void borderFontColor_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.ColorDialog colorPicker = new System.Windows.Forms.ColorDialog();
            if (colorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                borderFontColor.Background = new SolidColorBrush(Color.FromArgb(colorPicker.Color.A, colorPicker.Color.R, colorPicker.Color.G, colorPicker.Color.B));
        }
    }
}
